package com.lancoo.znbkxx.teacher.aibkTestCoach.questionToMark.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.lancoo.znbkxx.teacher.R;
import com.lancoo.znbkxx.teacher.utils.DensityUtils;
import com.lancoo.znbkxx.teacher.widget.bigimage.BigImageSizeCalculator;
import com.lancoo.znbkxx.teacher.widget.bigimage.BigImageZoomer;
import com.luck.picture.lib.entity.LocalMedia;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.SketchImageView;

/**
 * Created by Dadong on 2020/10/14
 * Describe:
 */
public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.ViewHolder> {

    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    private LayoutInflater mInflater;
    private List<LocalMedia> list = new ArrayList<>();
    private boolean isShowDelectIcon = true;
    private int selectMax = 9;
    private Context context;
    /**
     * 点击添加图片跳转
     */
    private onAddPicClickListener mOnAddPicClickListener;

    public interface onAddPicClickListener {
        void onAddPicClick();
    }

    public PhotoAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public PhotoAdapter(Context context, onAddPicClickListener mOnAddPicClickListener) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mOnAddPicClickListener = mOnAddPicClickListener;
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setList(List<LocalMedia> list) {
        this.list = list;
    }

    public boolean isShowDelectIcon() {
        return isShowDelectIcon;
    }

    public void setShowDelectIcon(boolean showDelectIcon) {
        isShowDelectIcon = showDelectIcon;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        SketchImageView mIvAdd;
        ImageView mIvPhotoLoading;

        public ViewHolder(View view) {
            super(view);
            mIvAdd = view.findViewById(R.id.iv_add);
            mIvAdd.setZoomEnabled(true);
            mIvAdd.getZoomer().setReadMode(true);
            Sketch.with(context).getConfiguration().setSizeCalculator(new BigImageSizeCalculator());
            mIvAdd.getZoomer().setZoomScales(new BigImageZoomer());
            mIvPhotoLoading = view.findViewById(R.id.iv_photoLoading);
        }
    }

    @Override
    public int getItemCount() {
        Log.e("==getItemCount==", "=============size==============" + list.size());
        if (isShowDelectIcon()) {
            if (list.size() < selectMax) {
                return list.size() + 1;
            } else {
                return list.size();
            }
        } else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.adapter_item_photo_picker,
                viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    private boolean isShowAddItem(int position) {
        int size = list.size() == 0 ? 0 : list.size();
        return position == size;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

//        DisplayMetrics dm = context.getResources().getDisplayMetrics();
//        int with = dm.widthPixels;
//        int viewWith = with * 1 / 3;
//        RelativeLayout.LayoutParams params_1= (RelativeLayout.LayoutParams) viewHolder.mIvAdd.getLayoutParams();
//        params_1.height=viewWith-40;
//        viewHolder.mIvAdd.setLayoutParams(params_1);

        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.mIvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.onAddPicClick();
                }
            });
        } else {

            LocalMedia media = list.get(position);
            Log.e("getPath", "==============media.getPath()=====mmdd=================" + media.getPath());
//            SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.mIvAdd.getLayoutParams();
//                    params.width = resource.getWidth();
//                    params.height = resource.getHeight();
//                    viewHolder.mIvAdd.setLayoutParams(params);
//                    viewHolder.mIvPhotoLoading.setVisibility(View.GONE);
//                }
//            };
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.mIvAdd.getLayoutParams();
            params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
            params.height = DensityUtils.dp2px(55f);
            viewHolder.mIvAdd.setLayoutParams(params);
            viewHolder.mIvPhotoLoading.setVisibility(View.GONE);
            viewHolder.mIvAdd.displayImage(media.getPath());
           /* Glide.with(context)
                    .asBitmap()
                    .load(media.getPath())
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull @NotNull Bitmap resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Bitmap> transition) {
                            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.mIvAdd.getLayoutParams();
                            params.width = resource.getWidth() * 3;
                            params.height = resource.getHeight() * 2;
                            viewHolder.mIvAdd.setLayoutParams(params);
                            viewHolder.mIvPhotoLoading.setVisibility(View.GONE);
                            viewHolder.mIvAdd.setImageBitmap(resource);
                        }

                        @Override
                        public void onLoadCleared(@Nullable @org.jetbrains.annotations.Nullable Drawable placeholder) {
                            viewHolder.mIvPhotoLoading.setVisibility(View.GONE);
                        }
                    });*/
                    /*.listener(new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {

                            viewHolder.mIvPhotoLoading.setVisibility(View.GONE);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target,
                                                       DataSource dataSource, boolean isFirstResource) {
                            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.mIvAdd.getLayoutParams();
//                            params.width = resource.getWidth() * 3;
//                            params.height = resource.getHeight() * 3;
                            params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
                            params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
                            viewHolder.mIvAdd.setLayoutParams(params);
                            viewHolder.mIvPhotoLoading.setVisibility(View.GONE);
                            return false;
                        }
                    })
                    .into(viewHolder.mIvAdd);*/
            //itemView 的点击事件
            if (mItemClickListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("==mItemClickListener==", "=======查看原图=====》》》》》》》====");
                        int adapterPosition = viewHolder.getAdapterPosition();
                        mItemClickListener.onItemClick(adapterPosition, v);
                    }
                });
            }
        }
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}